import React, {Component} from 'react';
import {
    View,
    Text,
    Alert
} from 'react-native';
//自定义组件
import Login from '../userCenter/login';
import UserCenter from './userCenter'
import MyStroage from '../common/myStroage';
import * as URLConstant from '../constant/URLconstant'
import Util from '../common/util'
import Toast from '../common/ToastUtil'

export default class userIndex extends Component {
    constructor(props) {
        super(props);
        // 初始状态
        this.successCallback = this.successCallback.bind(this);
        this.onLoginSuccess = this.onLoginSuccess.bind(this);
        this.errorcallback = this.errorcallback.bind(this);
        this.onRequestData = this.onRequestData.bind(this);
        this.onLoginSuccess1 = this.onLoginSuccess1.bind(this);
        this.onLogout=this.onLogout.bind(this)
        this.state = {
            datas: [],
            isLogin: false,
            loaded: true,
            titleType: 1,
        };
    }


    async getUser(){
        try {
            let that = this;
            let userInfoDate = await MyStroage.getStorageAllDataForKey('userinfo')
            LOG('KEY11111======' + JSON.stringify(userInfoDate));
            if (!userInfoDate) {
                that.setState({isLogin: false, loaded: false});
            } else {
                // let user_id = 6786589
                let user_id = userInfoDate.data.user_id;
                that.onRequestData(user_id);

            }

        } catch (err) {
            LOG(err);
        }
    }

    componentDidMount() {
        this.getUser()
    }


    //登录成功
    onLoginSuccess(user_id) {
        this.onRequestData(user_id)
        this.setState({
            loaded: true,
        })
    }

    //购物车操作调用
    onLoginSuccess1(user_id) {
        this.onRequestData(user_id);
    }

    async onRequestData(user_id) {
        try {
            //LOG("登录成功的user_id=====" + user_id);
            let datakey = [];
            if (user_id != null || user_id != '') {
                datakey['user_id'] = user_id;
                let MESSAGE_URL = URLConstant.USERINFO_URL;
                await Util.get(MESSAGE_URL, datakey, this.successCallback, this.errorcallback);
                this.setState({user_id: user_id});
            }

        } catch (e) {

        }
    }

    errorCallback(err) {

    }

    successCallback(datas) {
        let that = this;
        //LOG(datas)
        let data = datas.data;
        that.setState({
            data: data,
            loaded:false,
            isLogin: true
        })
    }

    errorcallback(err) {
        LOG('errorcallback==' + err);
    }


    onLogout(){
        this.setState({
            isLogin:false
        })
    }

    render() {
        let data = this.state.data;
        let user_id = this.state.user_id;
        return (
            <View style={{flex: 1}}>
                {this.state.loaded ?
                    <View style={{flex: 1}}>
                        {Util.loading()}
                    </View>
                    :
                    <View style={{flex: 1}}>
                        {this.state.isLogin ?
                            <View style={{flex: 1}}>
                                <UserCenter navigation={this.props.navigation} backHome={this.props.backHome}
                                            onLoginSuccess1={this.onLoginSuccess1} datas={{user_id, data}} onLogout={this.onLogout}/>
                            </View>
                            :
                            <Login navigation={this.props.navigation} from={1}
                                   onLoginSuccess={this.onLoginSuccess}/>
                        }
                    </View>
                }
            </View>
        )
    }
}
